using Project.Library.Core.DBC;

namespace Project.Library.NHibernate
{
    /// <summary>
    /// Static class that allow to work with a Unit of Work
    /// This class uses new operator and is the only one that must be rewrited
    /// </summary>
    /// <remarks>
    /// Use:
    /// <code>
    /// using(var UOW = NHUnitOfWork.Start())
    /// {
    ///     UOW.Transaction.CreateTransaction();
    ///     ...
    ///     UOW.Transaction.Commit();
    /// }
    /// 
    /// using(var UOW = NHUnitOfWork.Start())
    /// {
    ///     var repository = Container.Resolve<IRepository>();
    ///     ...
    ///     UOW.Transaction.TransactionalFlush();
    /// }
    /// </code>
    /// </remarks>
    public static class NHUnitOfWork
    {
        private static INHunitOfWorkFactory nhUOWFactory;
        public static INHUnitOfWork CurrentUOW{get;private set;}

        /// <summary>
        /// Creates a Unit of Work to start working with it.
        /// </summary>
        /// <returns></returns>
        public static INHUnitOfWork Start()
        {
            Checks.Require(CurrentUOW != null, "You are already in a unit of work");

            // Replace this holy sheet with a call to an IoC container
            nhUOWFactory = new NHunitOfWorkFactory(new NHSessionFactory(), new ActiveSessionManager(),
                                                   new TransactionManager());
            CurrentUOW = nhUOWFactory.Create();
            return CurrentUOW;
        }
    }
}